package com.hdyg.sixg.activity.home;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.WebActivity;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.AllMoneyMenuAdapter;
import com.hdyg.sixg.entry.NativeGuideBean;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.util.DataCenter;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author CZB
 * @describe 总钱包
 * @time 2019/4/17 11:04
 */
public class AllMoneyWalletActivity extends BaseActivity {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.rv_main)
    RecyclerView rvMain;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private AllMoneyMenuAdapter mAdapter;
    private HeadViewHolder headViewHolder;
    private View headView;
    private String allMoney;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recyclerview;
    }

    @Override
    protected void initView() {
        allMoney = getIntent().getStringExtra(SpMsg.INTENT_STR);
        rvMain.setLayoutManager(new LinearLayoutManager(mContext));
        tvTopTitle.setText(R.string.main_all_money);
        onClickListener();
        headView = StringUtil.getView(R.layout.item_top_all_money_wallet, mContext);
        headViewHolder = new HeadViewHolder(headView);
        headViewHolder.tvMoney.setText(allMoney);
    }

    private void onClickListener(){
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        llTopLeft.setOnClickListener(v -> {
            setResult(RESULT_OK);
            finish();
        });
    }

    @Override
    protected void initData() {
        initAdapter();
    }

    @Override
    protected void createPresenter() {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK){
            setResult(RESULT_OK);
            finish();
           return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initAdapter(){
        mAdapter = new AllMoneyMenuAdapter(R.layout.item_all_money_wallet,DataCenter.getAllWalletMenuData());
        mAdapter.addHeaderView(headView);
        rvMain.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (mAdapter.getData().get(position).getType() == NativeGuideBean.TYPE_1){
                startActivity(mAdapter.getData().get(position).getmClass());
            }else if (mAdapter.getData().get(position).getType() == NativeGuideBean.TYPE_2) {
                mBundle = new Bundle();
                mBundle.putString(SpMsg.URL, mAdapter.getData().get(position).getUrl());
                startActivity(WebActivity.class, mBundle);
            }else if (mAdapter.getData().get(position).getType() == NativeGuideBean.TYPE_3){
                toastNotifyShort(R.string.toast_development);
            }
        });
    }
    static class HeadViewHolder {

        @BindView(R.id.tv_money)
        TextView tvMoney;

        HeadViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
